package maple.thelittleredbook.content.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Param;
import maple.thelittleredbook.content.entity.ContentOperationLog;

import java.util.List;

/**
 * 内容操作日志Mapper接口
 */
@Mapper
public interface ContentOperationLogMapper extends BaseMapper<ContentOperationLog> {

    /**
     * 根据内容ID查询操作日志列表
     */
    @Select("SELECT id, content_id, operation_type, operation_status, total_operations, completed_operations, " +
            "failed_operations, error_message, started_at, completed_at, created_by " +
            "FROM content_operation_logs WHERE content_id = #{contentId} ORDER BY started_at DESC")
    List<ContentOperationLog> selectByContentId(@Param("contentId") Long contentId);

    /**
     * 根据内容ID和操作类型查询操作日志列表
     */
    @Select("SELECT id, content_id, operation_type, operation_status, total_operations, completed_operations, " +
            "failed_operations, error_message, started_at, completed_at, created_by " +
            "FROM content_operation_logs WHERE content_id = #{contentId} AND operation_type = #{operationType} " +
            "ORDER BY started_at DESC")
    List<ContentOperationLog> selectByContentIdAndType(@Param("contentId") Long contentId,
                                                       @Param("operationType") String operationType);

    /**
     * 检查内容是否有进行中的操作
     */
    @Select("SELECT COUNT(*) FROM content_operation_logs WHERE content_id = #{contentId} AND operation_status = 'PROCESSING'")
    Long countProcessingOperations(@Param("contentId") Long contentId);


    /**
     * 分页查询用户的操作日志
     * 对应XML文件中的selectPageByUserId
     *
     * @param page 分页参数
     * @param userId 用户ID
     * @param contentId 内容ID，可选
     * @param operationType 操作类型，可选
     * @return 分页操作日志记录
     */
    IPage<ContentOperationLog> selectPageByUserId(Page<ContentOperationLog> page,
                                                  @Param("userId") Long userId,
                                                  @Param("contentId") Long contentId,
                                                  @Param("operationType") String operationType);

    /**
     * 分页查询所有操作日志（管理员使用）
     * 对应XML文件中的selectPageForAdmin
     *
     * @param page 分页参数
     * @param userId 用户ID，可选
     * @param contentId 内容ID，可选
     * @param operationType 操作类型，可选
     * @return 分页操作日志记录
     */
    IPage<ContentOperationLog> selectPageForAdmin(Page<ContentOperationLog> page,
                                                  @Param("userId") Long userId,
                                                  @Param("contentId") Long contentId,
                                                  @Param("operationType") String operationType);
}